package com.turing.post.common.impl;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.List;

import javax.ejb.Local;
import javax.ejb.Remote;
import javax.ejb.Stateless;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.turing.post.common.util.CommonApplicationException;
import com.turing.post.common.util.IdType;
import com.turing.post.common.util.IdTypeDao;
import com.turing.post.organization.util.OrganApplicationException;
import com.turing.post.util.GeneralDao;
import com.turing.post.util.PCPSApplicationException;
import com.turing.post.util.PCPSUtil;

/**
 * @author zxp
 * @Dec 1, 2012
 */
@Stateless(mappedName = "common.impl.IdTypeDaoImpl")
@Remote
@Local
public class IdTypeDaoImpl implements IdTypeDao {

	/**
	 * 实体管理器
	 * 
	 * @see javax.persistence.EntityManager
	 */
	@PersistenceContext
	protected EntityManager _em;

	@Override
	public IdType getIdTypeByIdTypeId(BigInteger idTypeId) {
		return _em.find(IdType.class, idTypeId);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<IdType> getIdTypeIdList() {
		return _em.createNativeQuery("select * from IdType order By idTypeId",
				IdType.class).getResultList();
	}

	/**
	 * 根据类别查询证件类型列表
	 * 
	 * @param category
	 *            证件类型类别
	 * 
	 * @return 证件类型列表
	 */
	@SuppressWarnings("unchecked")
	public List<IdType> getIdTypeIdListByCategory(String category) {
		if (PCPSUtil.isEmpty(category))
			return getIdTypeIdList();
		return _em
				.createNativeQuery(
						"select t.* from IdType t where t.IdTypeCategory = ?1 order By IdTypeCategory, idTypeId",
						IdType.class).setParameter(1, category).getResultList();
	}

	@Override
	public BigInteger getMaxIdTypeId() {
		Query query = _em
				.createNativeQuery("select nvl(max(t.IdTypeId),0)+1 IdTypeId from IdType t");
		return new BigInteger(String.valueOf(((BigDecimal) query
				.getSingleResult())));
	}

	@Override
	public void insertIdType(IdType idType) throws PCPSApplicationException {
		if (idType != null) {
			idType.setIdTypeId(getMaxIdTypeId());
			if (PCPSUtil.isEmpty(idType.getIdTypeName())) {
				throw new CommonApplicationException(
						CommonApplicationException.ERRCODE_CATEGORY_APPLICATION,
						"", "证件类型名称不可为空!");
			}
			GeneralDao.insert(_em, idType);
		} else {
			throw new OrganApplicationException(
					CommonApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"", "IdType输入为NULL!");
		}
	}

	@Override
	public void updateIdType(IdType idType) throws PCPSApplicationException {
		IdType _idType = getIdTypeByIdTypeId(idType.getIdTypeId());
		if (_idType == null) {
			throw new OrganApplicationException(
					CommonApplicationException.ERRCODE_CATEGORY_APPLICATION,
					"", "待修改证件类型数据库中不存在!");
		}
		// 对象转换
		_idType.setIdTypeName(idType.getIdTypeName());
		_idType.setIdTypeCategory(idType.getIdTypeCategory());
		_idType.setIdTypeNo(idType.getIdTypeNo());
		GeneralDao.update(_em, _idType);
	}

	@SuppressWarnings("unchecked")
	@Override
	public IdType getIdTypeByIdTypeName(String idTypeName) {
		List<IdType> idTypeList = _em.createNativeQuery(
				"select * from IdType where IdTypeName =?1", IdType.class)
				.setParameter(1, idTypeName).getResultList();
		if (idTypeList == null || idTypeList.size() == 0) {
			return null;
		}
		return idTypeList.get(0);
	}

	/**
	 * 根据编号查询证件类型
	 * 
	 * @param idTypeNo
	 *            证件类型编号
	 * @throws PCPSApplicationException
	 */
	@SuppressWarnings( { "unchecked" })
	public IdType getIdTypeByIdTypeNo(String idTypeNo) {
		if (PCPSUtil.isEmpty(idTypeNo))
			return null;
		String sql = "select * from IdType where idTypeNo=?1";
		Query query = _em.createNativeQuery(sql, IdType.class);
		query.setParameter(1, idTypeNo);
		List<IdType> list = query.getResultList();
		IdType idType = null;
		if (list != null && list.size() > 0) {
			idType = list.get(0);
		}
		return idType;
	}
}
